package mhe.mgt;

import mhe.mgt.daos.GenericDAOImpl;
import mhe.mgt.service.*;

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {

	public static final String USER = "Admin";
	public static Shell mainShell;
	public static SystemService systemService = new SystemServiceImpl();
	public static MachineService machineService = new MachineServiceImpl();
	public static MachinePartService machinePartService = new MachinePartServiceImpl();


	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
	 */
	public Object start(IApplicationContext context) {
		Display display = PlatformUI.createDisplay();
		try {
			int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
			if (returnCode == PlatformUI.RETURN_RESTART) {
				return IApplication.EXIT_RESTART;
			}
			return IApplication.EXIT_OK;
		} finally {
			display.dispose();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	public void stop() {
		final IWorkbench workbench = PlatformUI.getWorkbench();
		if (workbench == null)
			return;
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable() {
			public void run() {
				if (!display.isDisposed())
					workbench.close();
			}
		});
		// shutdown database
		GenericDAOImpl.instance().shutDown();
	}
	
	public static void setStatusLine(String message) {
		IWorkbenchPartSite site = getIWorkbenchPartSite();
		if (site instanceof IViewSite) {
            ((IViewSite) site).getActionBars().getStatusLineManager().setMessage(message);
        }
	}
	
	public static void setStatusLine(Image image, String message) {
		IWorkbenchPartSite site = getIWorkbenchPartSite();
		if (site instanceof IViewSite) {
			((IViewSite) site).getActionBars().getStatusLineManager().setMessage(image, message);
		}
	}
	
	public static IWorkbenchPartSite getIWorkbenchPartSite() {
		return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().getActivePart().getSite();
	}

	public static Image getImage(String id) {
		return PlatformUI.getWorkbench().getSharedImages().getImage(id);
	}

	public static void openInformation(String title, String message) {
		MessageDialog.openInformation(mainShell, title, message);
	}

	public static void setStatusLineInfor(String message) {
		setStatusLine(Application.getImage(ISharedImages.IMG_OBJS_INFO_TSK), message);
	}

	public static void setStatusLineError(String message) {
		setStatusLine(Application.getImage(ISharedImages.IMG_OBJS_ERROR_TSK), message);
		
	}
}
